package com.hhit.bigscreenbackmanage.service.impl.jjgl;

import com.alibaba.fastjson.JSONObject;
import com.hhit.bigscreenbackmanage.entity.jjgl.TJjglJsxmJsgsje;
import com.hhit.bigscreenbackmanage.dao.jjgl.TJjglJsxmJsgsjeDao;
import com.hhit.bigscreenbackmanage.service.jjgl.TJjglJsxmJsgsjeService;
import com.hhit.bigscreenbackmanage.util.PageUtil;
import com.hhit.bigscreenbackmanage.vo.jjgl.JsVO;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 结算项目结算概算金额(TJjglJsxmJsgsje)表服务实现类
 *
 * @author makejava
 * @since 2021-07-21 10:49:06
 */
@Service("tJjglJsxmJsgsjeService")
public class TJjglJsxmJsgsjeServiceImpl implements TJjglJsxmJsgsjeService {
    @Resource
    private TJjglJsxmJsgsjeDao tJjglJsxmJsgsjeDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public TJjglJsxmJsgsje queryById(String id) {
        return this.tJjglJsxmJsgsjeDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @return 对象列表
     */
    @Override
    public Map<String, Object> queryAllByLimit(JSONObject jsonObject) {
        Map<String, Object> out = new HashMap<>();
        PageUtil page = new PageUtil();
        String pageSize = jsonObject.get("pageSize").toString();
        String pageNo = jsonObject.get("pageNo").toString();
        int count = tJjglJsxmJsgsjeDao.queryAllCount(jsonObject);
        page.pageCount = Integer.parseInt(pageSize);// 每页数
        page.init(count, Integer.parseInt(pageNo));//参数1：总行数，参数2：当前页
        jsonObject.put("rowNum", page.getRowNum());
        jsonObject.put("pageCount", page.getPageCount());
        out.put("page", page);
        out.put("list", tJjglJsxmJsgsjeDao.queryAll(jsonObject));
        return out;
    }

    /**
     * 新增数据
     *
     * @param tJjglJsxmJsgsje 实例对象
     * @return 实例对象
     */
    @Override
    public TJjglJsxmJsgsje insert(TJjglJsxmJsgsje tJjglJsxmJsgsje) {
        this.tJjglJsxmJsgsjeDao.insert(tJjglJsxmJsgsje);
        return tJjglJsxmJsgsje;
    }

    /**
     * 修改数据
     *
     * @param tJjglJsxmJsgsje 实例对象
     * @return 实例对象
     */
    @Override
    public TJjglJsxmJsgsje update(TJjglJsxmJsgsje tJjglJsxmJsgsje) {
        this.tJjglJsxmJsgsjeDao.update(tJjglJsxmJsgsje);
        return this.queryById(tJjglJsxmJsgsje.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.tJjglJsxmJsgsjeDao.deleteById(id) > 0;
    }

    @Override
    public Map<String, Object> getJgJs(JSONObject jsonObject) {
        Map<String, Object> out = new HashMap<>();

        JsVO jgJs = tJjglJsxmJsgsjeDao.getJgJs();
        // 按期数
        String aqs = jgJs.getAqs();
        // 结算数
        String jss = jgJs.getJss();
        // 结算金额
        String jsTz = sanNum(jgJs.getJsTz());
        out.put("jss", jss);
        out.put("jsTz", jsTz);
        out.put("jsaqwcl", oneNum(chuFa(aqs, jss)));
        return out;
    }

    @Override
    public Map<String, Object> getJjglJgjs(JSONObject jsonObject) throws Exception {
        Map<String, Object> out = new HashMap<>();
//        List<Map<String, Object>> jsxmList = new ArrayList<>();
//        List<Map<String, Object>> jsjeList = new ArrayList<>();
        List<List<String>> chartData = new ArrayList<>();
        List<String> jhlist = new ArrayList<>();
        List<String> sjlist = new ArrayList<>();
        List<String> gslist = new ArrayList<>();
        List<String> jslist = new ArrayList<>();
        // 结算数
        int jsxms = tJjglJsxmJsgsjeDao.getJss();
        // 结算金额
        Double jszje = tJjglJsxmJsgsjeDao.getJstz();
        // 概算金额
        Double gszje = tJjglJsxmJsgsjeDao.getGstz();
        // 结算超概算比例
        Double jstzcgstzbl = tJjglJsxmJsgsjeDao.getJstzcgstzbl();
        //竣工结算
//        List<Map<String,Object>> jsxmAndJsjeList = tJjglJsxmJsgsjeDao.getJsxmAndJsjeData(jsonObject);
        List<Map<String, Object>> jsxmAndJsjeList_jh = tJjglJsxmJsgsjeDao.getJsxmAndJsjeDataNew_jh(jsonObject);
        List<Map<String, Object>> jsxmAndJsjeList_sj = tJjglJsxmJsgsjeDao.getJsxmAndJsjeDataNew_sj(jsonObject);
        String[] dydjs = {"500kV","220kV","110kV","35kV"};
        for (int i = 0; i < dydjs.length; i++) {
            String dydj1 = dydjs[i];
            for (Map map : jsxmAndJsjeList_jh) {
                if (dydj1.equals(map.get("dydj"))) {
//                    Map<String, Object> jsxmMap = new HashMap<>();
//                    jsxmMap.put("total",map.get("bnum"));
//                    jsxmMap.put("value",map.get("anum"));
//                    if(Integer.parseInt(map.get("bnum").toString())==0&&Integer.parseInt(map.get("anum").toString())==0){
//                        jsxmMap.put("percent",0.00);
//                        jsxmMap.put("notNumValue","");
//                    }else if(Integer.parseInt(map.get("bnum").toString())==0&&Integer.parseInt(map.get("anum").toString())!=0){
//                        jsxmMap.put("percent",100);
//                    }else{
//                        BigDecimal xmbl = new BigDecimal(map.get("anum").toString()).divide(new BigDecimal(map.get("bnum").toString())).multiply(new BigDecimal(100));
//                        jsxmMap.put("percent",xmbl);
//                    }
//                    jsxmList.add(jsxmMap);
//                    Map<String, Object> jsjeMap = new HashMap<>();
//                    jsjeMap.put("total",map.get("bjstz"));
//                    jsjeMap.put("value",map.get("ajstz"));
//                    if(Double.valueOf(map.get("bjstz").toString())==0&&Double.valueOf(map.get("ajstz").toString())==0){
//                        jsjeMap.put("percent",0.00);
//                        jsjeMap.put("notNumValue","");
//                        jsjeMap.put("label","");
//                    }else if(Double.valueOf(map.get("bjstz").toString())==0&&Double.valueOf(map.get("ajstz").toString())!=0){
//                        jsjeMap.put("percent",100);
//                        jsjeMap.put("label","");
//                    }else{
//                        BigDecimal xmbl = new BigDecimal(map.get("ajstz").toString()).divide(new BigDecimal(map.get("bjstz").toString())).multiply(new BigDecimal(100));
//                        jsjeMap.put("percent",xmbl);
//                    }
//                    jsjeList.add(jsjeMap);
                    if ("35kV".equals(dydj1)) {
                        jhlist.add(map.get("num").toString());
                    } else if ("110kV".equals(dydj1)) {
                        jhlist.add(map.get("num").toString());
                    } else if ("220kV".equals(dydj1)) {
                        jhlist.add(map.get("num").toString());
                    } else if ("500kV".equals(dydj1)) {
                        jhlist.add(map.get("num").toString());
                    }
                } else {
                    continue;
                }
            }
        }
        for (int i = 0; i < dydjs.length; i++) {
            String dydj1 = dydjs[i];
            for (Map map : jsxmAndJsjeList_sj) {
                if ("35kV".equals(dydj1)&&dydj1.equals(map.get("dydj"))) {
                    sjlist.add(map.get("num").toString());
                    gslist.add(map.get("gsdttz").toString());
                    jslist.add(map.get("jsdttz").toString());
                } else if ("110kV".equals(dydj1)&&dydj1.equals(map.get("dydj"))) {
                    sjlist.add(map.get("num").toString());
                    gslist.add(map.get("gsdttz").toString());
                    jslist.add(map.get("jsdttz").toString());
                } else if ("220kV".equals(dydj1)&&dydj1.equals(map.get("dydj"))) {
                    sjlist.add(map.get("num").toString());
                    gslist.add(map.get("gsdttz").toString());
                    jslist.add(map.get("jsdttz").toString());
                } else if ("500kV".equals(dydj1)&&dydj1.equals(map.get("dydj"))) {
                    sjlist.add(map.get("num").toString());
                    gslist.add(map.get("gsdttz").toString());
                    jslist.add(map.get("jsdttz").toString());
                }
            }
        }
        chartData.add(jhlist);
        chartData.add(sjlist);
        chartData.add(gslist);
        chartData.add(jslist);
        out.put("jsxms", jsxms);
        out.put("jszje", jszje);
        out.put("gstz", gszje);
        out.put("jstzcgstzbl", jstzcgstzbl);
        out.put("chartData", chartData);
//        out.put("jsxmBarData",jsxmList);
//        out.put("jsjeBarData",jsjeList);
        return out;
    }

    /**
     * 除法
     *
     * @param a
     * @param b
     * @return
     */
    public static String chuFa(String a, String b) {
        //“0.0000”确定精度
        DecimalFormat dF = new DecimalFormat("0.0000");
        if (StringUtils.isEmpty(a)) {
            a = "0";
        }
        if (StringUtils.isEmpty(b) || "0".equals(b)) {
            return "0";
        }
        float af = Float.parseFloat(a);
        float bf = Float.parseFloat(b);
        return dF.format(af / bf);
    }

    /**
     * @param a
     * @return
     */
    public static String sanNum(String a) {
        if (StringUtils.isEmpty(a)) {
            a = "0";
        }
        float af = Float.parseFloat(a);
        //“0.0000”确定精度
        DecimalFormat dF = new DecimalFormat("0.000");
        return dF.format(af);
    }

    /**
     * @param a
     * @return
     */
    public static String oneNum(String a) {
        if (StringUtils.isEmpty(a)) {
            a = "0";
        }
        float af = Float.parseFloat(a);
        //“0.0000”确定精度
        DecimalFormat dF = new DecimalFormat("0.0");
        return dF.format(af * 100);
    }

}